//
//  XKVisitorView.swift
//  XKWeibo
//
//  Created by 小k on 15/11/4.
//  Copyright © 2015年 小k. All rights reserved.
//

import UIKit
import SnapKit

// /// 设置访客视图的协议
//protocol XKVisitorViewDelegate: NSObjectProtocol {
//    // 注册
//    func visitorViewRegister()
//    // 登陆
//    func visitorViewLogin()
//}


class XKVisitorView: UIView {
    
//    /// 设置代理属性
//    weak var delegate:XKVisitorViewDelegate?
//    
//    /// 监听方法
//    @objc func clickRegister() {
//        
//        delegate?.visitorViewRegister()
//    }
//    
//    @objc func clickLogin () {
//        delegate?.visitorViewLogin()
//    }
    
    
    // MARK: - 视图信息
    func setupInfo (iconName:String?, textName:String) {
        
        
        // 如果图片 为空  直接返回, 说明是主页
        guard let imgName = iconName else {
            startAnim()
            return
        }
        
        messageLabel.text = textName
        
        iconImgView.image = UIImage(named: imgName)
        
        //隐藏遮罩
        maskImgView.hidden = true
        //隐藏小房子
        houseImgView.hidden = true
    }
    
    // MARK: - 设置首页动画
    private func startAnim () {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 10              // 持续时间
        anim.repeatCount = MAXFLOAT

        //不要移除动画  ---> 视图切换,就会销毁
        anim.removedOnCompletion = false
        
        // 添加动画到图层
        iconImgView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }

    
 
    // MARK: - 懒加创建控件
    
    // 图标 使用image 构造方法创建的 默认
    private lazy var iconImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 小房子
    private lazy var houseImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 遮罩图片
    private lazy var maskImgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 消息文字
    private lazy var messageLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    
    /**
    *  创建注册按妞
    */
    
    lazy var registerBtn:UIButton = UIButton(title: "注册",color:UIColor.orangeColor(), imageName: "common_button_white_disable")
    
    /**
    *  创建登录按钮
    */
    
    lazy var longinBtn:UIButton = UIButton(title: "登陆",color:UIColor.orangeColor(), imageName: "common_button_white_disable")
    
}

extension XKVisitorView {
    
    // MARK: - 设置界面
    private func setupUI () {
        /// 1. 添加控件
        addSubview(iconImgView)
        addSubview(maskImgView)
        addSubview(houseImgView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(longinBtn)
        
        /// 2. 设置 frame 自动布局
        // 设置子控件可以 使用 自动布局
        for view in subviews {
            view.translatesAutoresizingMaskIntoConstraints = false // 默认 为true 使用setFrame 布局
        }
        
        // 旋转图标
        iconImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        // 小房子
        houseImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImgView.snp_centerX)
            make.centerY.equalTo(iconImgView.snp_centerY)
        }

        // 消息文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImgView.snp_centerX)
            make.top.equalTo(iconImgView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        
        // 注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        
        // 登录按钮
        longinBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(registerBtn.snp_size)
        }
        
        
        // 遮罩图片
        maskImgView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top)
            make.right.equalTo(snp_right)
            make.left.equalTo(snp_left)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
//        // 添加监听方法
//        registerBtn.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
//        longinBtn.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)
        
    }
    
    
    
}

